-- cmd_buy_food_material
-- Create by huanghr
-- 购买试剂

function Operation.cmd_buy_food_material(id, num)
    -- 客户端先自己购买
    local price = ItemM.query(id, "buy_price");
    if type(price) ~= "table" or #price <= 0 then
        return;
    end
    price = price[1];

    -- 暂时默认只有属性
    local attrib = price[2];
    local amount = price[3] * num;
    if ME.user:queryAttrib(attrib) < amount then
        return;
    end

    -- 扣除属性
    ME.user:costAttrib(attrib, amount);

    -- 奖励
    BonusM.doBonus({ 1, id, num }, "buy_material");

    -- 抛出事件
    EventMgr.fire(event.BUY_FOOD_MATERIAL, { ["classId"] = id, ["amount"] = num });

    local v = {
        ["material_id"] = id,
        ["amount"]      = num,
    };

    SyncM.addMessage("CMD_BUY_FOOD_MATERIAL", v);

    -- TODO:
    SyncM.startSync();
end
